package com.jzy.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jzy.constant.MessageConstant;
import com.jzy.constant.RedisConstant;
import com.jzy.entity.PageResult;
import com.jzy.entity.QueryPageBean;
import com.jzy.entity.Result;
import com.jzy.pojo.CheckGroup;
import com.jzy.pojo.Setmeal;
import com.jzy.service.SetmealService;
import com.jzy.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 体检套餐管理
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Reference
    private SetmealService setmealService;

    //使用JedisPool操作Redis服务
    @Autowired
    private JedisPool jedisPool;

    //文件上传
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile) {
        //获取文件扩展名
        String originalFilename = imgFile.getOriginalFilename();//原始文件名:xx.x.jpg
        int index = 0;
        if (originalFilename != null) {
            index = originalFilename.lastIndexOf(".");//获取最后一个点的位置
        }
        String extension = null;
        if (originalFilename != null) {
            extension = originalFilename.substring(index);//截取后缀: .jpg
        }

        //通过UUID生成一个随机的为文件名
        String fileName = UUID.randomUUID() + extension;

        //通过自定义七牛云工具类上传文件
        try {
            //将文件上床到七牛云服务器
            QiniuUtils.upload2Qiniu(imgFile.getBytes(), fileName);
            //将上传图片名称存入Redis，基于Redis的Set集合存储
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES, fileName);
        } catch (IOException e) {
            e.printStackTrace();
            new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
        return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, fileName);
    }

    //新增
    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
        try {
            setmealService.add(setmeal, checkgroupIds);
        } catch (Exception e) {
            //新增套餐失败
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
        //新增套餐成功
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = setmealService.pageQuery(queryPageBean);
        return pageResult;
    }

    //根据ID删除
    @RequestMapping("/delete")
    public Result delete(@RequestParam("id") Integer setmealId) {
        try {
            setmealService.deleteById(setmealId);
        } catch (Exception e) {
            //新增删除失败
            return new Result(false, MessageConstant.DELETE_SETMEAL_FAIL);
        }
        //新增删除成功
        return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
    }

    //根据套餐ID查套餐内容
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Setmeal checkGroup = setmealService.findById(id);
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, checkGroup);//查询套餐成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);//查询套餐失败
        }
    }

    //根据套餐ID查套餐包含的多个检查组ID
    @RequestMapping("/finCheckGroupIdsBySeatmealId")
    public Result finCheckGroupIdsBySeatmealId(Integer id) {
        try {
            List<Integer> checkGroupIds = setmealService.finCheckItemIdsByCheckGroupId(id);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroupIds);//查询检查组成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);//查询检查组失败
        }
    }

    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
        try {
            setmealService.edit(setmeal, checkgroupIds);
        } catch (Exception e) {
            //新增套餐失败
            return new Result(false, MessageConstant.EDIT_SETMEAL_FAIL);
        }
        //新增套餐成功
        return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
    }

}